import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(MainState state, Dispatch dispatch, ViewService viewService) {

  return Scaffold(
    appBar: AppBar(
      title: Row(
        mainAxisSize: MainAxisSize.min,
        children: _createTitleWidgets(state, dispatch, viewService),
      ),
      actions: <Widget>[
        IconButton(
          icon: Icon(
            Icons.settings,
            color: Colors.white,
          ),
          onPressed: () {
            print('set company info');
          },
        ),
      ],
    ),
    body: viewService.buildComponent('village-info'),
  );
}

List<Widget> _createTitleWidgets(
    MainState state, Dispatch dispatch, ViewService viewService) {
  var containerList = List<Widget>();
  containerList.add(Container(
    child: DropdownButton(
      underline: Text(''),
      value: state.selectedCompany != null
          ? state.selectedCompany.companyId
          : null,
      items: _createCompanyList(state),
      onChanged: (companyId) {
        dispatch(MainActionCreator.switchCompany(companyId));
      },
      isExpanded: true,
    ),
    width: 120,
    height: 32,
  ));
  if (state.selectedCompany != null) {
    containerList.add(Text('>>'));
    containerList.add(Container(
      child: DropdownButton(
        underline: Text(''),
        value: state.selectedVillage != null
            ? state.selectedVillage.villageId
            : null,
        items: _createVillageList(state),
        onChanged: (villageId) {
          dispatch(MainActionCreator.switchVillage(villageId));
        },
        isExpanded: true,
      ),
      width: 80,
      height: 32,
    ));
  }
  return containerList;
}

List<DropdownMenuItem> _createCompanyList(MainState state) {
  var containerList = List<DropdownMenuItem>();
  var selectedId =
      state.selectedCompany != null ? state.selectedCompany.companyId : -1;
  state.commanyList.forEach((company) {
    var bgColor =
        selectedId == company.companyId ? Colors.blue : Colors.transparent;
    containerList.add(
      DropdownMenuItem(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(
              company.companyName,
              style: TextStyle(fontSize: 16, backgroundColor: bgColor),
              maxLines: 1,
            ),
          ],
        ),
        value: company.companyId,
      ),
    );
  });
  return containerList;
}

List<DropdownMenuItem> _createVillageList(MainState state) {
  var list = List<DropdownMenuItem>();
  var selectedId =
      state.selectedVillage != null ? state.selectedVillage.villageId : -1;
  state.villageList.forEach((company) {
    var bgColor =
        selectedId == company.villageId ? Colors.blue : Colors.transparent;
    list.add(DropdownMenuItem(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            company.villageName,
            style: TextStyle(fontSize: 14, backgroundColor: bgColor),
            maxLines: 1,
          ),
        ],
      ),
      value: company.villageId,
    ));
  });
  return list;
}
